package com.test.mytest.saolei.my.swt;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

/*
 * Created on 2004-8-6
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author TdbT
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
class LeftClick extends SelectionAdapter {
    Data[][] minedata = new Data[9][9];
    Button[][] minebtn = new Button[9][9];
    Button start;
    Display display;
    Composite mineplace;
    int i;
    int j;
    public LeftClick(Mine mines, Display display,int i, int j,Button start) {
        this.minebtn = mines.minebtn;
        this.minedata = mines.minedata;
        mineplace=mines.mineplace;
        this.display = display;
        this.i=i;
        this.j=j;
        this.start=start;
    }
    public void setBtnImage() {
        
                if (minedata[i][j].getMineNum()>0&&!minedata[i][j].isMine()){
                    ImageData imdt = new ImageData(getClass()
                            .getResourceAsStream(minedata[i][j].getImage()));
                    Image icon = new Image(display, imdt);
                    minebtn[i][j].setImage(icon);
                    minebtn[i][j].setSelection(true);
                }
                else if(minedata[i][j].isMine()){
                    ImageData imdt = new ImageData(getClass()
                            .getResourceAsStream("mine2.gif"));
                    Image icon = new Image(display, imdt);
                    minebtn[i][j].setImage(icon);
                    minebtn[i][j].setSelection(true);
                    mineExplode();
                    Miner.start=false;
                    ImageData imdt2 = new ImageData(getClass()
                            .getResourceAsStream("face2.gif"));
                    Image icon2 = new Image(display, imdt2);
                    start.setImage(icon2);
                    mineplace.setEnabled(false);
                }
                else if(minedata[i][j].getMineNum()==0){
                    findVoid(i,j);
               }
            
    }
    public void mineExplode(){
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++){
                
if(minedata[i][j].isMine()&&minedata[i][j].getLeftClickNum()==0&&minedata[i][
j].getRightClickNum()!=1){
                    minedata[i][j].responseLeftClick();
                    ImageData imdt = new ImageData(getClass()
                            .getResourceAsStream("mine1.gif"));
                    Image icon = new Image(display, imdt);
                    minebtn[i][j].setImage(icon);
                    minebtn[i][j].setSelection(true);
                }
                
if(!minedata[i][j].isMine()&&minedata[i][j].getLeftClickNum()==0&&minedata[i]
[j].getRightClickNum()==1){
                    minedata[i][j].responseLeftClick();
                    ImageData imdt = new 
ImageData(getClass().getResourceAsStream("wrong.gif"));
                    Image icon = new Image(display, imdt);
                    minebtn[i][j].setImage(icon);
                    minebtn[i][j].setSelection(true);
                }

        }
    }
    public void findVoid(int i,int j){
        if(minedata[i][j].getMineNum()==0){
            ImageData imdt = new ImageData(getClass()
                    .getResourceAsStream(minedata[i][j].getImage()));
            Image icon = new Image(display, imdt);
            minebtn[i][j].setImage(icon);
            minebtn[i][j].setSelection(true);
            
if(i-1>=0&&j-1>=0&&minedata[i-1][j-1].getLeftClickNum()==0&&minedata[i-1][j-1
].getRightClickNum()==0)
            {
                minedata[i-1][j-1].responseLeftClick();
                imdt = new ImageData(getClass()
                        
.getResourceAsStream(minedata[i-1][j-1].getImage()));
                icon = new Image(display, imdt);
                minebtn[i-1][j-1].setImage(icon);
                minebtn[i-1][j-1].setSelection(true);
                findVoid(i-1,j-1);
            }
            
if(i-1>=0&&minedata[i-1][j].getLeftClickNum()==0&&minedata[i-1][j].getRightClickNum()==0){
                minedata[i-1][j].responseLeftClick();
                imdt = new ImageData(getClass()
                        .getResourceAsStream(minedata[i-1][j].getImage()));
                icon = new Image(display, imdt);
                minebtn[i-1][j].setImage(icon);
                minebtn[i-1][j].setSelection(true);
                findVoid(i-1,j);
            }
            
if(i-1>=0&&j+1<9&&minedata[i-1][j+1].getLeftClickNum()==0&&minedata[i-1][j+1]
.getRightClickNum()==0){
                minedata[i-1][j+1].responseLeftClick();
                imdt = new ImageData(getClass()
                        
.getResourceAsStream(minedata[i-1][j+1].getImage()));
                icon = new Image(display, imdt);
                minebtn[i-1][j+1].setImage(icon);
                minebtn[i-1][j+1].setSelection(true);
                findVoid(i-1,j+1);
            }
            
if(j-1>=0&&minedata[i][j-1].getLeftClickNum()==0&&minedata[i][j-1].getRightClickNum()==0){
                minedata[i][j-1].responseLeftClick();
                imdt = new ImageData(getClass()
                        .getResourceAsStream(minedata[i][j-1].getImage()));
                icon = new Image(display, imdt);
                minebtn[i][j-1].setImage(icon);
                minebtn[i][j-1].setSelection(true);
                findVoid(i,j-1);
            }
            
if(j+1<9&&minedata[i][j+1].getLeftClickNum()==0&&minedata[i][j+1].getRightClickNum()==0){
                minedata[i][j+1].responseLeftClick();
                imdt = new ImageData(getClass()
                        .getResourceAsStream(minedata[i][j+1].getImage()));
                icon = new Image(display, imdt);
                minebtn[i][j+1].setImage(icon);
                minebtn[i][j+1].setSelection(true);
                findVoid(i,j+1);
            }
            
if(j-1>=0&&i+1<9&&minedata[i+1][j-1].getLeftClickNum()==0&&minedata[i+1][j-1]
.getRightClickNum()==0){
                minedata[i+1][j-1].responseLeftClick();
                imdt = new ImageData(getClass()
                        
.getResourceAsStream(minedata[i+1][j-1].getImage()));
                icon = new Image(display, imdt);
                minebtn[i+1][j-1].setImage(icon);
                minebtn[i+1][j-1].setSelection(true);
                findVoid(i+1,j-1);
            }
            
if(i+1<9&&minedata[i+1][j].getLeftClickNum()==0&&minedata[i+1][j].getRightClickNum()==0){
                minedata[i+1][j].responseLeftClick();
                imdt = new ImageData(getClass()
                        .getResourceAsStream(minedata[i+1][j].getImage()));
                icon = new Image(display, imdt);
                minebtn[i+1][j].setImage(icon);
                minebtn[i+1][j].setSelection(true);
                findVoid(i+1,j);
            }
            
if(i+1<9&&j+1<9&&minedata[i+1][j+1].getLeftClickNum()==0&&minedata[i+1][j+1].
getRightClickNum()==0){
                minedata[i+1][j+1].responseLeftClick();
                imdt = new ImageData(getClass()
                        
.getResourceAsStream(minedata[i+1][j+1].getImage()));
                icon = new Image(display, imdt);
                minebtn[i+1][j+1].setImage(icon);
                minebtn[i+1][j+1].setSelection(true);
                findVoid(i+1,j+1);
            }
                
        }
        }
    public void widgetSelected(SelectionEvent e) {
        if(Miner.start==false)
            Miner.start=true;
        if(minedata[i][j].getRightClickNum()!=1){
        minedata[i][j].responseLeftClick();
        setBtnImage();
        }
        else
            minebtn[i][j].setSelection(false);
    }
} 